﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;
using DataAccess;
using KhoaLuan.Utils;
using log4net;

namespace KhoaLuan.MainSys.ProcessAJAX
{
    public partial class AjaxIntroduce : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string AddNew(int introid, string title, string content)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementIntroduce.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.AddNew))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền thêm giới thiệu" });
            try
            {
                var user = UserLogin.GetCurrentUser;
                var org = UserLogin.GetCurrentOrg;
                var proxyintro = new tbIntroduce_Proxy();
                if (introid == 0)
                {
                    var intro = new tbIntroduce()
                    {
                        Title = title,
                        OrgID = org.OrgID,
                        ContenIntroduce = HttpUtility.HtmlEncode(content)
                    };
                    if (proxyintro.Insert_tbIntroduce_Proxy(intro))
                    {
                        return new JavaScriptSerializer().Serialize(new { Status = true });
                    }
                    else
                        throw new Exception();
                }
                else
                {
                    var intro = proxyintro.GetById_tbIntroduce_Proxy(introid).ToObject<tbIntroduce>();
                    if (intro == null)
                        throw new Exception();
                    if (intro.OrgID != org.OrgID)
                        throw new Exception();
                    intro.ContenIntroduce = HttpUtility.HtmlEncode(content);
                    intro.Title = title;
                    if (!proxyintro.Update_tbIntroduce_Proxy(intro))
                        throw new Exception();
                    return new JavaScriptSerializer().Serialize(new { Status = true });
                }
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }


        [WebMethod]
        public static string GetListIntro(int currentpage, int numberinpage, string keyword)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementIntroduce.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.View))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xem danh sách giới thiệu" });
            try
            {
                var org = UserLogin.GetCurrentOrg;
                if (org == null)
                    throw new Exception();
                tbIntroduce_Proxy pr = new tbIntroduce_Proxy();
                int totalitem = 0;
                var list = pr.GetIntroByOrg(org.OrgID);
                if (keyword != null && keyword != "")
                {
                    keyword = keyword.ToLower();
                    list = list.Where(x => x.Title.ToLower().Contains(keyword) || x.ContenIntroduce.ToLower().Contains(keyword)).ToList();
                }
                totalitem = list.Count;
                var list2 = list.Skip((currentpage - 1) * numberinpage).Take(numberinpage);
                var result = list2.Select(x => new { x.IntroduceId, x.Title, x.OrgID, x.IsIntro });
                return new JavaScriptSerializer().Serialize(new { Status = true, Data = result, TotalItem = totalitem, CurrentPage = currentpage });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }

        public object DeleteIntroduce(int IntroduceId)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementIntroduce.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Delete))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xóa giới thiệu" });
            try
            {
                tbIntroduce_Proxy pr = new tbIntroduce_Proxy();
                if (pr.Delete_tbIntroduce_Proxy(IntroduceId))
                    return new JavaScriptSerializer().Serialize(new { Status = "Success" });
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }
        public object InsertIntroduce(string Title, string ContenIntroduce, int OrgID, bool IsIntro)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementIntroduce.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.AddNew))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền thêm giới thiệu" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbIntroduce).Name);
                tbIntroduce_Proxy pr = new tbIntroduce_Proxy();
                DataAccess.tbIntroduce a = new DataAccess.tbIntroduce();
                a.IntroduceId = 0;
                a.Title = Title;
                a.ContenIntroduce = ContenIntroduce;
                a.OrgID = OrgID;
                a.IsIntro = IsIntro;
                if (pr.Insert_tbIntroduce_Proxy(a))
                {
                    log.Info(UserLogin.GetUserName + "Thêm mới Introduce thành công");
                    return new JavaScriptSerializer().Serialize(new { Status = "Success" });
                }
                else
                    return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }

        public object UpdateIntroduce(int IntroduceId, string Title, string ContenIntroduce, int OrgID, bool IsIntro)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementIntroduce.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Edit))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền chỉnh sửa giới thiệu" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbIntroduce).Name);
                tbIntroduce_Proxy pr = new tbIntroduce_Proxy();
                DataAccess.tbIntroduce a = new DataAccess.tbIntroduce();
                a.IntroduceId = IntroduceId;
                a.Title = Title;
                a.ContenIntroduce = ContenIntroduce;
                a.OrgID = OrgID;
                a.IsIntro = IsIntro;
                if (pr.Update_tbIntroduce_Proxy(a))
                    log.Info(UserLogin.GetUserName + "Cập nhật Introduce thành công");
                return new JavaScriptSerializer().Serialize(new { Status = "Success" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }

    }
}